package cn.migu.music.hangupsms.repository;

import cn.migu.music.hangupsms.common.entity.ViewVrbtSettings;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ViewVrbtSettingsMapper {
    @Insert(value = "insert into t_migu_hangup_view_vrbt_setting" +
                    "(tone_id,provinces,view_seconds,calling_msg,msg_port,last_operator,operate_channel,create_time,update_time)" +
                    " values (#{settings.toneId},#{settings.provinces},#{settings.viewSeconds},#{settings.callingMsg},#{settings.msgPort},#{settings.lastOperator}" +
                    ",#{settings.operateChannel},now(),now())")
    void insert(@Param("settings") ViewVrbtSettings settings);

    @Update(value = "<script>update t_migu_hangup_view_vrbt_setting set \n" +
                    "  <if test=\"settings.provinces != null\">\n" +
                    "  provinces=#{settings.provinces},\n" +
                    "  </if>\n" +
                    "  <if test=\"settings.viewSeconds != null\">\n" +
                    "  view_seconds=#{settings.viewSeconds},\n" +
                    "  </if>\n" +
                    " <if test=\"settings.callingMsg != null\">\n" +
                    "  calling_msg=#{settings.callingMsg},\n" +
                    "  </if>\n" +
                    " <if test=\"settings.msgPort != null\">\n" +
                    "  msg_port=#{settings.msgPort},\n" +
                    "  </if>\n" +
                    " <if test=\"settings.lastOperator != null\">\n" +
                    "  last_operator=#{settings.lastOperator},\n" +
                    "  </if>\n" +
                    "  <if test=\"settings.operateChannel != null\">" +
                    "  operate_channel=#{settings.operateChannel}," +
                    "  </if>" +
                    "  update_time=now()\n" +
                    " where tone_id=#{settings.toneId}</script>")
    void update(@Param("settings") ViewVrbtSettings settings);

    @Delete(value = "delete from t_migu_hangup_view_vrbt_setting where tone_id=#{toneId}")
    void delete(@Param("toneId") String toneId);

    @Select({ "select * from t_migu_hangup_view_vrbt_setting where tone_id=#{toneId}" })
    List<ViewVrbtSettings> getByToneId(@Param("toneId") String toneId);

    /**
     * 分页查询
     *
     * @param toneId
     * @param start
     * @param size
     * @return
     */
    @Select({ "<script>",
              "select * from t_migu_hangup_view_vrbt_setting where 1=1 ",
              "<if test='toneId!=null'>",
              " and tone_id=#{toneId}",
              "</if>",
              " order by update_time desc",
              " offset #{start} limit #{size}",
              "</script>" })
    List<ViewVrbtSettings> getByConditionByPage(@Param("toneId") String toneId,
                                                @Param("start") Integer start,
                                                @Param("size") Integer size);

    /**
     * 计算总数
     *
     * @param toneId
     * @return
     */
    @Select({ "<script>",
              "select count(*) from t_migu_hangup_view_vrbt_setting where 1=1 ",
              "<if test='toneId!=null'>",
              " and tone_id=#{toneId}",
              "</if>",
              "</script>" })
    int countByCondition(@Param("toneId") String toneId);
}
